import java.util.*;
import java.io.*;

public class Finder
{
	public static void main(String[] args) throws Exception
	{
		Thread.currentThread().setPriority(10);
		if (args.length < 2)
			System.out.println("Usage: java Finder [directory] [filename extension] [<array>excludes]");
		else
		{
			System.out.print("Search for: ");
			Scanner scanner = new Scanner(System.in);
			String toFind = scanner.nextLine();
			long startTime = System.currentTimeMillis();
			int countFiles = 0, totalCount = 0;
			List<Object[]> linesList = new ArrayList<Object[]>();
			List<String> filesList = new ArrayList<String>();
			File[] files = new File(args[0]).listFiles();
			master: for (int i = 0; i < files.length; i++)
			{
				if (!files[i].getName().endsWith(args[1]))
					continue;
				for (int j = 2; j < args.length - 2; j++)
					if (files[i].getName().substring(0, files[i].getName().lastIndexOf('.')).equalsIgnoreCase(args[j]))
						continue master;
				FileInputStream fis = new FileInputStream(files[i]);
				byte[] buffer = new byte[fis.available()];
				fis.read(buffer);
				fis.close();
				String s = new String(buffer);
				if (s.contains(toFind))
				{
					countFiles++;
					filesList.add(files[i].getName());
					List<Integer> tempLinesList = new ArrayList<Integer>();
					StringTokenizer tokener = new StringTokenizer(s, "\r\n");
					for (int k = 1; tokener.hasMoreTokens(); k++)
					{
						String next = tokener.nextToken();
						if (next.contains(toFind))
						{
							tempLinesList.add(new Integer(k));
							totalCount++;
						}
					}
					linesList.add(tempLinesList.toArray());
				}
			}
			String print = "\r\n";
			for (int i = 0; i < filesList.size(); i++)
			{
				String filename = filesList.get(i);
				Object[] lines = linesList.get(i);
				for (int j = 0; j < lines.length; j++)
					print += "Message found in " + filename + " on line " + ((Integer)lines[j]).intValue() + ".\r\n";
			}
			if (print.equalsIgnoreCase("\r\n"))
				print += "Message not found.";
			else
				print += "\r\nMessage found in " + countFiles + " files with a total of " + totalCount + " results in "
				+ (System.currentTimeMillis() - startTime) + " milliseconds.";
			System.out.println(print);

			/*	String[] fileNames = new File("./", args[0]).list();
			for (int i = 0; i < fileNames.length; i++)
				if (!fileNames[i].endsWith("." + args[1]))
					for (int j = i + 1; j < fileNames.length; j++)
						fileNames[i] = fileNames[j];
			for (int i = 0; i < fileNames.length; i++)
			{
				String name = fileNames[i];
				boolean bool = false;
				for (int j = 2; j < args.length; j++)
				{
					bool = name.equals(args[j] + "." + args[1]);
					if (bool) break;
				}
				if (bool) continue;
				File f = new File("./" + args[0] + "/", fileNames[i]);
				FileInputStream fis = new FileInputStream(f);
				byte[] buffer = new byte[(int)f.length()];
				fis.read(buffer);
				fis.close();
				String content = new String(buffer);
				if (content.contains(toFind))
				{
					System.out.println("Found message inside " + args[1] + " file " + fileNames[i]);
					found = true;
					num++;
				}
			}
			if (!found)
				System.out.println("Could not find " + toFind);
			else
				System.out.println(num + " results found in " + (System.currentTimeMillis() - start) + " milliseconds");*/

			FileOutputStream fos = new FileOutputStream("Scan.txt");
			fos.write(print.getBytes());
			fos.flush();
			fos.close();

		}
	}
}